package my.diplom.web.validators;

import org.springframework.validation.Validator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.apache.log4j.Logger;
import my.diplom.entity.Message;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 05.09.2009
 * Time: 1:40:11
 * To change this template use File | Settings | File Templates.
 */
public class CreateMessageValidator implements Validator {

     private static final Logger log = Logger.getLogger(CreateMessageValidator.class);

    @Override
    public boolean supports(Class clazz) {
        return Message.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Message message = (Message) target;
        if (message == null) errors.reject("Null data received");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "title.empty", "Title is required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "body", "body.empty", "Body is required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "toUser", "toUser.empty", "User is required");
    }
}
